package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {
    @Reference(version = "1.0.0")
    private OrderSettingService orderSettingService;

    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        try {
            ArrayList<OrderSetting> orderSettings = new ArrayList<>();
            List<String[]> list = POIUtils.readExcel(file);
            for (String[] strings : list) {
                Date date = new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]);
                Integer number = Integer.parseInt(strings[1]);
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(number);
                orderSetting.setOrderDate(date);
                orderSetting.setReservations(0);
                orderSettings.add(orderSetting);
            }
            orderSettingService.add(orderSettings);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(-999, "文件上传失败");
    }

    @GetMapping("/find")
    public Result find(String month) {
        return orderSettingService.findByMonth(month);
    }

    @GetMapping("/updateNumber")
    public Result updateNumber(String date,Integer number) throws ParseException {
        return orderSettingService.updateNumber(date,number);
    }



}
